package tp.pr5.commands;

import java.util.StringTokenizer;

import tp.pr5.Game;
import tp.pr5.Map;
import tp.pr5.MensajesConst;
import tp.pr5.Player;
import tp.pr5.Room;
import tp.pr5.commands.exceptions.CommandExecutionException;
import tp.pr5.commands.exceptions.WrongCommandFormatException;
import tp.pr5.items.Item;

public class PickCommand implements Command
{

	private String _itemToPickId;
	private Player _friki;
	private Game _game;
	private Room _habitacion;
	private Map _mapa;
	public void configureContext(Game g, Map m, Player p) {
		this._friki=p;
		this._habitacion=m.getCurrentRoom();
		this._game=g;
		this._mapa=m;
	}
	@Override public void execute() throws CommandExecutionException
	{
		Item playerItem = this._friki.getItem(this._itemToPickId);
		
		if (playerItem != null)
			throw new CommandExecutionException("You have another "
					+ this._itemToPickId + "in your inventory\n ");

		if (!(this._habitacion.existsItem(this._itemToPickId)))
			throw new CommandExecutionException(MensajesConst.cantPick);
		this._game.getCurrentRoom().pickItem(this._friki,
				this._itemToPickId);
		//TODO nuevo!
		this._friki.requestInventoryUpdate();
		this._mapa.playerExamineRoom();
	}

	@Override public String getHelp()
	{
		return MensajesConst.hPick;
	}

	@Override public Command parse(String cad)
			throws WrongCommandFormatException
	{
		StringTokenizer tkn = new StringTokenizer(cad);
		if (tkn.countTokens() != 2)
			throw new WrongCommandFormatException();

		String verb = tkn.nextToken();
		if (!(verb.toUpperCase().equalsIgnoreCase("PICK") || verb.toUpperCase()
				.equalsIgnoreCase("COGER")))
			throw new WrongCommandFormatException();
		this._itemToPickId = tkn.nextToken();
		//this._game=execContext;
		//this._friki=this._game.getPlayer();
		return this;

	}
}
